<template>
	<view class="login">
		<view class="TopBox">
			<image class="loginTopBg" src="/static/loginTopBg.png" mode="widthFix"></image>
			<image class="logo" src="/static/logo2.png" mode="aspectFill"></image>
		</view>
		<view class="content">
			<view class="title">{{plat_name}}</view>
			
			<view class="form">
				<view class="item">
					<view class="label">手机号</view>
					<u-input v-model="form.phone" placeholder="请输入手机号" type="text"></u-input>
				</view>
				<view class="item" v-if="false">
					<view class="label">用户密码</view>
					<u-input v-model="form.password" placeholder="请输入用户密码" type="password"></u-input>
				</view>
				<view class="item">
					<view class="label">验证码</view>
					<u-input v-model="form.code" placeholder="请输入验证码"></u-input>
					<u-button class="code" type="success" hover-class="none" size="mini" @click="getCode">{{codeTips}}</u-button>
				</view>
			</view>
			<view class="operate flex_space_b" v-if="false">
				<view class="loginText">注册</view>
				<view class="forget">忘记密码</view>
			</view>
			
			<view class="protocolText">
				<u-checkbox v-model="protocol" class="protocol" active-color="#fe0000" icon-size="24">
					<view>
						<text>请认真查阅</text>
						<text style="color: rgb(41, 121, 255);padding: 10rpx;" @click.stop="popupShow = true">《用户协议》</text>
						<text>并同意</text>
					</view>
				</u-checkbox>
			</view>
			<view class="buttonBox">
				<u-button type="primary" :disabled="!protocol" hover-class="none" @click="phone_login">立即登录</u-button>
			</view>
			
			<view class="OR" v-if="false">
				<image class="image" src="/static/OR.png" mode="widthFix"></image>
			</view>
			<view class="platform mTop2" v-if="false">
				<u-icon name="qq-fill" size="48" color="#acacac"></u-icon>
				<u-icon name="weixin-fill" size="48" color="#acacac"></u-icon>
				<u-icon name="weibo" size="48" color="#acacac"></u-icon>
			</view>
		</view>
		
		<u-popup v-model="popupShow" mode="center" width="640" border-radius="12" :closeable="true">
			<view class="popupBox">
				<view class="title">规则介绍</view>
				<scroll-view scroll-y class="contentBox">
					<u-parse :html="html"></u-parse>
				</scroll-view>
			</view>
		</u-popup>
		
		<u-verification-code seconds="60" ref="uCode" @change="codeChange"></u-verification-code>
	</view>
</template>

<script>
	var util = require('../../common/util.js');
	export default {
		data() {
			return {
				form: {
					phone: '',
					password: '',
					code: '',
					invite_user_id:0
				},
				protocol: false,
				codeTips: '',
				popupShow: false,
				html: '',
				is_jump:'0',
				plat_name:uni.getStorageSync('xixuan_plat_name')
			}
		},
		onLoad:function(op){
			if(op.invite_user_id){
				this.form.invite_user_id = op.invite_user_id;
			}
			if(op.is_jump){
				this.is_jump = op.is_jump;
			}
			if(uni.getStorageSync("xixuan_pintuan_token")){
				uni.switchTab({
					url:"../index/index"
				});
			}else{
				this.loadData();
			}
		},
		methods: {
			loadData: function(){
				// 加载数据
				var that = this;
				util.requestPost("api.php/login/app_agreement", {}, function(res) {
					that.html = res.data.app_agreement;
				});
			},
			phone_login(){
				let that = this;
				util.requestPost("api.php/login/phone_login", {phone:that.form.phone,code:that.form.code,invite_user_id:that.form.invite_user_id}, function(res) {
					// 存储手机号
					uni.setStorageSync("xixuan_pintuan_phone",that.form.phone);
					uni.setStorageSync("xixuan_pintuan_token",res.data.token);
					if(that.is_jump == '10'){
						uni.navigateBack();
					}else{
						uni.switchTab({
							url:"../index/index"
						});
					}
					
				},true);
			},
			codeChange(text) {
				this.codeTips = text;
			},
			// 获取验证码
			getCode() {
				if(this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码',
						mask: true
					});
					var that = this;
					util.requestPost("api.php/sms/sendCodeSms", {user_phone:this.form.phone,event:'login'}, function(res) {
						uni.hideLoading();
						// 这里此提示会被this.start()方法中的提示覆盖
						that.$u.toast(res.msg);
						// 通知验证码组件内部开始倒计时
						that.$refs.uCode.start();
					});
					
				} else {
					this.$u.toast('倒计时结束后再发送');
				}
			},
		}
	}
</script>

<style scoped>
.login {
	min-height: 100%;
	background-color: var(--bgColor);
}
.TopBox {
	position: relative;
}
.TopBox .loginTopBg {
	width: 750rpx;
}
.TopBox .logo {
	width: 148rpx;
	height: 148rpx;
	border: 6rpx solid #ffa3a3;
	border-radius: 100%;
	margin-top: 110rpx;
	position: absolute;
	left: 50%;
	transform: translateX(-50%);
}
.content {
	position: relative;
	z-index: 4;
	margin: -136rpx 32rpx 0;
	padding: 52rpx 46rpx;
	box-sizing: border-box;
	border-radius: 40rpx;
	background-color: #FFFFFF;
}
.title {
	text-align: center;
	font-size: 48rpx;
	font-weight: 600;
}

.form {
	margin-top: 52rpx;
}
.form .item {
	background-color: #fafafa;
	border-radius: 12rpx;
	padding: 12rpx 32rpx 12rpx;
	display: flex;
	justify-content: center;
	align-items: center;
}
.form .item:nth-of-type(n+2) {
	margin-top: 32rpx;
}
.form .item .label {
	width: 108rpx;
	color: #7b807d;
	font-size: 26rpx;
	margin-right: 16rpx;
}
.form .item .u-input {
	font-size: 24rpx;
	flex: 1;
}
.form .item .code {
	background-color: var(--primaryColor);
}
.operate {
	margin-top: 26rpx;
	padding: 0 12rpx;
	font-size: 24rpx;
}
.operate .loginText {
	color: rgb(41, 121, 255);
}
.operate .forget {
	color: #999999;
}

.protocolText {
	margin-top: 32rpx;
	margin-left: 20rpx;
}
.buttonBox {
	margin-top: 32rpx;
}
.buttonBox .u-btn {
	background-color: var(--primaryColor);
	border-radius: 50rpx;
	height: 90rpx;
	box-shadow: 0 0 16rpx rgba(254, 0, 0, 0.2);
}
.buttonBox .u-btn.u-btn--primary--disabled {
	border-color: #fec2c2 !important;
	background-color: #fec2c2 !important;
}

.OR {
	margin-top: 32rpx;
	padding: 0 32rpx;
}
.OR .image {
	width: 100%;
}
.platform {
	display: flex;
	align-items: center;
	justify-content: center;
}
.platform .u-icon:nth-of-type(2) {
	margin: 0 80rpx;
}

.popupBox {
	padding-bottom: 28rpx;
}
.popupBox .title{
	padding: 0 70rpx;
	text-align: center;
	line-height: 88rpx;
	font-size: 32rpx;
	font-weight: 600;
}
.popupBox .contentBox {
	box-sizing: border-box;
	padding: 0 20rpx;
	max-height: 800rpx;
}
</style>
